package com.txc.books.controller;

import com.txc.books.entity.LoanRecords;

import com.txc.books.exception.BaseException;
import com.txc.books.service.ILoanRecordsService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import org.springframework.web.bind.annotation.*;

/**
 * @author: XCTANG
 * 借阅记录表控制类
 */


@RestController
@RequestMapping("/loan")
public class LoanRecordsController {

    private ILoanRecordsService loanRecordsService;

    public LoanRecordsController(ILoanRecordsService loanRecordsService){
        this.loanRecordsService = loanRecordsService;
    }

    //查询所有记录
    @GetMapping(value = "/findAll")
    public void findAll(){
        loanRecordsService.findAll();
    }

    //添加借阅记录
    @PostMapping(value = "/insert")
    public void addLoan(LoanRecords loanRecords){

        try{
            loanRecordsService.insert(loanRecords);
        }catch (BaseException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }

    //更新借阅记录
    @PostMapping(value = "/update")
    public void updateLoan(LoanRecords loanRecords){

        try{
            loanRecordsService.update(loanRecords);
        }catch (BaseException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }


    //删除借阅记录
    @GetMapping(value = "/delete")
    public void deleteLoan(@RequestParam("id") String id){
        loanRecordsService.delete(Integer.parseInt(id.trim()));
    }

    //根据id查询借阅记录
    @GetMapping(value = "/select")
    public void select(@RequestParam("id") String id){
        loanRecordsService.select(Integer.parseInt(id));
    }

    //根据图书名字查询借阅记录
    @GetMapping(value = "/selectByBookName")
    public void selectByBookName(@RequestParam("bName") String bName){
        loanRecordsService.selectByBookName(bName);
    }

    //根据借阅人名查询借阅记录
    @GetMapping(value = "/selectByMemberName")
    public void selectByMemberName(@RequestParam("name") String name){
        loanRecordsService.selectByMemberName(name);
    }

    //根据借阅开始时间查询借阅记录
    @PostMapping(value = "/selectByTime")
    public void selectByTime(String createTime){
        loanRecordsService.selectByTime(createTime);
    }

    //根据借阅状态查询借阅记录
    @GetMapping(value = "/selectByStauts")
    public void selectByStauts(Integer loanStauts){
        loanRecordsService.selectByStauts(loanStauts);
    }

    //查询可以借阅的图书
    @GetMapping(value = "/selectLoan")
    public void selectLoan(@RequestParam("id") String id){
        loanRecordsService.selectLoan(Integer.parseInt(id));
    }

    //分页查询
    @GetMapping(value="/findPage")
    public PageResult findPage(PageRequest pageQuery) {
        return loanRecordsService.findByPager(pageQuery);
    }

    //借阅图书
    @PostMapping("/borrow")
    public void borrowBook(LoanRecords loanRecords){
        try{
            loanRecordsService.borrowBook(loanRecords);
        }catch (BaseException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }

    //归还图书
    @PostMapping("/return")
    public void returnBook(LoanRecords loanRecords){
        try{
            loanRecordsService.returnBook(loanRecords);
        }catch (BaseException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }
}
